home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH06 / PGM6_4.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-10-19  |  2.6 KB  |  118 lines

  1. ; Shift and Rotate Instructions
  2.  
  3.         .386            ;So we can use extended registers
  4.         option    segment:use16    ; and addressing modes.
  5.  
  6. dseg        segment    para public 'data'
  7.  
  8. ; The following structure holds the bit values for an 80x86 mod-reg-r/m byte.
  9.  
  10. mode        struct
  11. modbits        byte    ?
  12. reg        byte    ?
  13. rm        byte    ?
  14. mode        ends
  15.  
  16. Adrs1        mode    {11b, 100b, 111b}
  17. modregrm    byte    ?
  18.  
  19. var1        word    1
  20. var2        word    8000h
  21. var3        word    0FFFFh
  22. var4        word    ?
  23.  
  24. dseg        ends
  25.  
  26. cseg        segment    para public 'code'
  27.         assume    cs:cseg, ds:dseg
  28.  
  29. Main        proc
  30.         mov    ax, dseg
  31.         mov    ds, ax
  32.         mov    es, ax
  33.  
  34. ; Shifts and rotates directly on memory locations:
  35. ;
  36. ; var1 := var1 shl 1
  37.  
  38.         shl    var1, 1
  39.  
  40. ; var1 := var1 shr 1
  41.  
  42.         shr    var1, 1
  43.  
  44. ; On 80286 and later processors, you can shift by more than one bit at
  45. ; at time:
  46.  
  47.         shl    var1, 4
  48.         shr    var1, 4
  49.  
  50. ; The arithmetic shift right instruction retains the H.O. bit after each
  51. ; shift.  The following SAR instruction sets var2 to 0FFFFh
  52.  
  53.         sar    var2, 15
  54.  
  55. ; On all processors, you can specify a shift count in the CL register.
  56. ; The following instruction restores var2 to 8000h:
  57.  
  58.         mov    cl, 15
  59.         shl    var2, cl
  60.  
  61. ; You can use the shift and rotate instructions, along with the logical
  62. ; instructions, to pack and unpack data.  For example, the following
  63. ; instruction sequence extracts bits 10..13 of var3 and leaves
  64. ; this value in var4:
  65.  
  66.         mov    ax, var3
  67.         shr    ax, 10        ;Move bits 10..13 to 0..3.
  68.         and    ax, 0Fh        ;Keep only bits 0..3.
  69.         mov    var4, ax
  70.  
  71. ; You can use the rotate instructions to compute this value somewhat faster
  72. ; on older processors like the 80286.
  73.  
  74.         mov    ax, var3
  75.         rol    ax, 6        ;Six rotates rather than 10 shifts.
  76.         and    ax, 0Fh
  77.         mov    var4, ax
  78.  
  79. ; You can use the shift and OR instructions to easily merge separate fields
  80. ; into a single value.  For example, the following code merges the mod, reg,
  81. ; and r/m fields (maintained in separate bytes) into a single mod-reg-r/m
  82. ; byte:
  83.  
  84.  
  85.         mov    al, Adrs1.modbits
  86.         shl    al, 3
  87.         or    al, Adrs1.reg
  88.         shl    al, 3
  89.         or    al, Adrs1.rm
  90.         mov     modregrm, al
  91.  
  92. ; If you've only got and 8086 or 8088 chip, you'd have to use code like the
  93. ; following:
  94.  
  95.         mov    al, Adrs1.modbits    ;Get mod field
  96.         shl    al, 1
  97.         shl    al, 1
  98.         or    al, Adrs1.reg        ;Get reg field
  99.         mov    cl, 3
  100.         shl    al, cl            ;Make room for r/m field.
  101.         or    al, Adrs1.rm        ;Merge in r/m field.
  102.         mov    modregrm, al        ;Save result away.
  103.  
  104. Quit:        mov    ah, 4ch            ;DOS opcode to quit program.
  105.         int    21h            ;Call DOS.
  106. Main        endp
  107.  
  108. cseg        ends
  109.  
  110. sseg        segment    para stack 'stack'
  111. stk        byte    1024 dup ("stack   ")
  112. sseg        ends
  113.  
  114. zzzzzzseg    segment    para public 'zzzzzz'
  115. LastBytes    byte    16 dup (?)
  116. zzzzzzseg    ends
  117.         end    Main
  118.